<?php

namespace app\api\controller\system;


use app\api\controller\Api;
use app\common\config\logic\DictionaryLogic;
use app\common\config\validate\ConfigRegionValidate;
use app\common\system\logic\FormLogic;
use app\common\config\model\ConfigRegion;
use util\Tree;

class Region extends Api
{
    public function tree()
    {
        $model = new ConfigRegion();
        $rows = $model->where('is_valid', 1)->order('sort,id')->select();
        if (!$rows->isEmpty()) {
            $list = $rows->toArray();
            $result['tree'] = Tree::buildTree2([], $list, 0);
        } else {
            $result['tree'] = [];
        }

        return success($result);
    }

    public function getProvinceList(): \think\response\Json
    {
        $model = new ConfigRegion();
        $rows = $model->where('level', 1)->order('sort,id')->select();
        if (!$rows->isEmpty()) {
            $result['list'] = $rows;
        } else {
            $result['list'] = [];
        }

        return success($result);
    }

    // 获取已经开放的城市列表
    public function getOpenedCityList(): \think\response\Json
    {
        $model = new ConfigRegion();
        $rows = $model->field("id,shortname")->where('level', 2)->where('is_valid', 1)->order('sort,id')->select();
        if (!$rows->isEmpty()) {
            $result['list'] = $rows;
        } else {
            $result['list'] = [];
        }

        return success($result);
    }

    public function getChildren(): \think\response\Json
    {
        $post = $this->request->post();
        if (empty($post['id'])) {
            return error([], "参数错误");
        }

        if (isset($post['is_open']) && $post['is_open'] == 1) {
            $validWhere['is_valid'] = 1;
        } else {
            $validWhere['is_valid'] = 0;
        }

        $model = new ConfigRegion();
        $rows = $model->where('pid', $post['id'])->where($validWhere)->order('sort,id')->select();
        if (!$rows->isEmpty()) {
            $result['list'] = $rows;
        } else {
            $result['list'] = [];
        }

        return success($result);
    }

    public function read(): \think\response\Json
    {
        $post = $this->request->post();
        if (empty($post['id'])) return error([], '参数错误');

        $model = new ConfigSource();
        $result['row'] = $model->where('id', $post['id'])->find();

        return success($result);
    }

    public function openCity()
    {
        $post = $this->request->post();
        if (empty($post['city_id'])) {
            return error([], "参数错误");
        }

        $model = new ConfigRegion();
        $rs = $model->where('id', $post['province_id'])->whereOr('id', $post['city_id'])->whereOr('pid', $post['city_id'])->save(['is_valid' => 1]);
        if ($rs) {
            return success([], "开启城市成功");
        } else {
            return error([], "开启城市失败");
        }
    }

    public function closeCity()
    {
        $post = $this->request->post();
        if (empty($post['id'])) {
            return error([], "参数错误");
        }

        $model = new ConfigRegion();
        $row = $model->where('id', $post['id'])->find();
        if (empty($row)) {
            return error([], "信息不存在");
        }

        $rs = 0;
        if ($row['level'] == '1') {
            $rows = $model->where('pid', $row['id'])->where('is_valid', 1)->select();
            if (!$rows->isEmpty()) {
                return error([], "该省份还有未关闭的城市");
            }
            $rs = $model->where('id', $row['id'])->save(['is_valid' => 0]);
        }

        if ($row['level'] == '2') {
            $rs = $model->where('id', $row['id'])->whereOr('pid', $row['id'])->save(['is_valid' => 0]);
        }

        if ($rs > 0) {

            return success([], "操作成功");
        } else {
            return error([], "操作失败");
        }

    }

    public function save()
    {
        $post = $this->request->post();

        // 验证提交
        $validate = new ConfigRegionValidate();
        if (!$validate->scene('edit')->check($post)) return error([], $validate->getError());

        $model = new ConfigRegion();
        $row = $model->where('id', $post['id'])->find();
        if (empty($row)) return error([], '信息已不存在');
        $rs = $row->save($post);

        if ($rs) {
            // 核销表单token
            $formLogic = new FormLogic();
            $formLogic->removeToken($post['form_token']);
            // 更新缓存
            $dictionaryLogic = new DictionaryLogic();
            $dictionaryLogic->resetItemCache($post['id'], ConfigRegion::class);
            return success([], '操作成功');
        } else {
            return error([], '操作失败');
        }
    }

}